package com.android.cookbook.adapter;

import java.io.IOException;
import java.util.ArrayList;

import com.android.cookbook.R;
import com.android.cookbook.R.drawable;
import com.android.cookbook.R.id;
import com.android.cookbook.R.layout;
import com.android.cookbook.activity.ListDriver;
import com.android.cookbook.bean.Type;
import com.android.cookbook.db.BaseDatosHelper;
import com.android.cookbook.util.Constant;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ImageAdapter extends BaseAdapter{  

	Context mContext;  
	BaseDatosHelper myDBHelper;
	ArrayList<Type> typeList;
	
	public ImageAdapter(Context c){  
		mContext = c;  
		createDB();
		this.typeList = getTypes();
    }  

    public int getCount() {  
    	return typeList.size();  
    }  

    public Object getItem(int position) {
        return position;
    }
    
    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {  
    	View v = convertView; 
		if (v == null) {                 
			LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
			v = vi.inflate(R.layout.type_list, null);             
		}
		TextView tv = (TextView)v.findViewById(R.id.type_text);
		ImageView iv = (ImageView)v.findViewById(R.id.type_image);
		
		Type type = (Type)typeList.get(position);
		tv.setText(type.getName()); 
		
    	switch (position) { 
    		case 0:
    			iv.setImageResource(R.drawable.ensalada); 
        		break;
    		case 1:
    			iv.setImageResource(R.drawable.postre);
    			break;
    		case 2:
    			iv.setImageResource(R.drawable.my_recipes);
    			break;
    		case 3:
    			iv.setImageResource(R.drawable.favorites);
    			break;	
    		default: 
        } 
    	
		iv.setTag(type);
		iv.setOnClickListener(new View.OnClickListener() {             
        	public void onClick(View view) {                   
        		Bundle bundle = new Bundle();
        		Type myType = (Type)view.getTag();
        		bundle.putString(Constant.TYPE_ID,String.valueOf(myType.get_id())); 
        		bundle.putString(Constant.TYPE_NAME,myType.getName());
        		bundle.putString(Constant.TYPE_SEARCH,Constant.LIST_BY_ID);
        		Intent myIntent = new Intent(view.getContext(),ListDriver.class);
        		myIntent.putExtras(bundle);                   
        		mContext.startActivity(myIntent);             
        	}          
        });
        return v;  
    }  
    
	public void createDB() {         
		myDBHelper = new BaseDatosHelper(mContext);         
		try {             
			myDBHelper.createDataBase();         
		} catch (IOException ioe) {             
			throw new Error("Unable to create database");         
		}     
	}

	public ArrayList<Type> getTypes() {         
		myDBHelper.openDataBase();         
		ArrayList<Type> typeList = myDBHelper.getTypeList();         
		myDBHelper.close();         
		return typeList;     
	}

}  

